import 'package:flutter/services.dart';
import 'dart:convert';
import '../manger/user_manger.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';

class NumberUtils {
static String formatDecimalValue(double value) {
  // 判断当前是否为中文环境
  if (isChineseLocale()) {
    if (value % 1 == 0) {
      return value.toInt().toString() + '折'; // 中文环境，显示折扣
    }
    String formatted = value.toStringAsFixed(2);
    if (formatted.endsWith('0')) {
      formatted = formatted.substring(0, formatted.length - 1);
      if (formatted.endsWith('0')) {
        formatted = formatted.substring(0, formatted.length - 2);
      }
    }
    return '$formatted折'; // 中文环境，显示折扣
  } else {
    // 非中文环境，显示 6% off
    String formatted = (100 - (value * 10)).toStringAsFixed(0); // 假设 value 为 9.4 折，折扣就是 100 - 94 = 6
    return '$formatted% off'; // 英文环境，显示折扣百分比
  }
}


  static Future<String> getAreaCode() async {
    try {
      final country = UserInfos.shared.userModel?.countryEn;
      if (country != null && country.isNotEmpty) {
        final jsonString = await rootBundle.loadString('assets/json/area.json');
        final List<dynamic> decode = json.decode(jsonString);
        for (var areaCode in decode) {
          final labelParts = areaCode['label'].split(' ');
          if (labelParts[0] == country) {
            return areaCode['label'];
          }
        }
      }
    } catch (e) {
      print('读取本地数据出现错误! $e');
    }
    return 'US +1';
  }
}

/// 判断保存的时间戳是否超过一小时
Future<bool> checkIfTimestampExceededOneHour() async {
  final prefs = await SharedPreferences.getInstance();
  final savedTimestamp = prefs.getString('savedTimestamp');

  if (savedTimestamp != null) {
    final savedTimeInterval = int.tryParse(savedTimestamp);
    if (savedTimeInterval != null) {
      final currentTimestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
      final elapsedTime = currentTimestamp - savedTimeInterval;
      return elapsedTime > 3600; // 3600秒 = 1小时
    }
  }
  return true;
}

/// 保存当前时间戳到savedTimestamp
Future<void> saveCurrentTimestamp() async {
  final prefs = await SharedPreferences.getInstance();
  final currentTimestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
  await prefs.setString('savedTimestamp', currentTimestamp.toString());
}

/// 保存当前时间戳到savedTimestamp2
Future<void> saveCurrentTimestamp2() async {
  final prefs = await SharedPreferences.getInstance();
  final currentTimestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
  await prefs.setString('savedTimestamp2', currentTimestamp.toString());
}

/// 判断保存的时间戳是否超过一天
Future<bool> checkIfTimestampExceededOneDay() async {
  final prefs = await SharedPreferences.getInstance();
  final savedTimestamp = prefs.getString('savedTimestamp2');

  if (savedTimestamp != null) {
    final savedTimeInterval = int.tryParse(savedTimestamp);
    if (savedTimeInterval != null) {
      final currentTimestamp = DateTime.now().millisecondsSinceEpoch ~/ 1000;
      final elapsedTime = currentTimestamp - savedTimeInterval;
      return elapsedTime > 86400; // 86400秒 = 1天
    }
  }
  return true;
}
